﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace RSSReader
{
    public class Rss
    {
        public static void ReadItems(TreeNode node, List<RssItem> channels)
        {
            //블로그 제목을 열었을때 포스트의 제목을 가져 옵니다.
            XmlTextReader reader = new XmlTextReader(channels[node.Index].Link);

            reader.ReadToDescendant("item");

            if (IsElement(reader, "item"))
                node.Nodes.Add(GetTreeNode(reader));


            while (reader.ReadToNextSibling("item"))
                if (IsElement(reader, "item"))
                    node.Nodes.Add(GetTreeNode(reader));

            if (HasChild(node))
                channels[node.Index].LastPost = node.Nodes[0].Name.ToString();

            reader.Close();
        }

        private static TreeNode GetTreeNode(XmlReader reader)
        {
            TreeNode node = new TreeNode();
            XmlReader subReader = reader.ReadSubtree();

            while (subReader.Read())
            {
                CheckAndRead(subReader, "title", node.Text);
                CheckAndRead(subReader, "link", node.Name);
                CheckAndRead(subReader, "description", (string)node.Tag);
            }

            return node;
        }

        private static void CheckAndRead(XmlReader reader, string name, string target)
        {
            if (reader.Name == name && reader.NodeType == System.Xml.XmlNodeType.Element)
                target = reader.ReadString();
        }

        private static bool IsElement(XmlReader reader, string name)
        {
            if (reader.Name == name && reader.NodeType == System.Xml.XmlNodeType.Element)
                return true;

            return false;
        }

        private static bool HasChild(TreeNode node)
        {
            if (node.Nodes.Count != 0 &&
                node.Nodes[0].Name != null &&
                node.Nodes[0].Name.ToString() != "")
                return true;

            return false;
        }
    }

    public class RssItem
    {
        #region <<< [ Variables    ] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

        private string _title = "";
        private string _link = "";
        private string _description = "";
        private string _lastPost = "";
        private bool _isRead = false;

        #endregion


        #region <<< [ Properties   ] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public string Link
        {
            get { return _link; }
            set { _link = value; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public string LastPost
        {
            get { return _lastPost; }
            set { _lastPost = value; }
        }

        public bool IsRead
        {
            get { return _isRead; }
            set { _isRead = value; }
        }

        #endregion


        #region <<< [ Constructors ] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

        public RssItem()
        {
        }

        public RssItem(System.Xml.XmlReader reader)
        {
            ReadXml(reader);
        }

        public RssItem(string title, string link, string description)
        {
            this._title = title;
            this._link = link;
            this._description = description;
        }

        #endregion


        #region <<< [ Methods      ] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

        public void ReadXml(System.Xml.XmlReader reader)
        {
            System.Xml.XmlReader subReader = reader.ReadSubtree();

            while (subReader.Read())
            {
                CheckAndRead(subReader, "title", _title);
                CheckAndRead(subReader, "link", _link);
                CheckAndRead(subReader, "description", _description);
            }
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("item");

            CheckAndWrite(writer, "title", _title);
            CheckAndWrite(writer, "link", _link);
            CheckAndWrite(writer, "description", _description);

            writer.WriteEndElement();
        }
                    
        private void CheckAndRead(System.Xml.XmlReader reader, string name, string target)
        {
            if (reader.Name == name && reader.NodeType == System.Xml.XmlNodeType.Element)
                target = reader.ReadString();
        }

        private void CheckAndWrite(System.Xml.XmlWriter writer, string localName, string value)
        {
            if (value != null && value != "")
                writer.WriteElementString(localName, value);
        }

        

        #endregion
    }
}
